/**
 * Created by Administrator on 2016/11/4.
 */
$(function(){

    var map = new BMap.Map("allmap");    // 创建Map实例
    map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_SATELLITE_MAP ]}));
    map.enableScrollWheelZoom(true);
    map.enableScrollWheelZoom();
    map.addEventListener("tilesloaded",function(){
        $(".anchorBL").remove();
    });
    var ctrl = new BMapLib.TrafficControl({
        showPanel: true //是否显示路况提示面板
    });
    map.addControl(ctrl);
    ctrl.setAnchor(BMAP_ANCHOR_TOP_LEFT);
    map.addControl(new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_RIGHT, mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]}));
    var driving = new BMap.DrivingRoute(map, {
        policy : BMAP_DRIVING_POLICY_LEAST_DISTANCE,
        onSearchComplete: function(results){
            if (driving.getStatus() == BMAP_STATUS_SUCCESS){
                // 获取第一条方案
                var plan = results.getPlan(0);
                distance = plan.getDistance(true);
                timeStr = plan.getDuration(true);
                //线路
                var  pis = plan.getRoute(0).getPath();
             //   var polyline = new BMap.Polyline(plan,{strokeColor:"#3366ff",strokeWeight:2});
              //  map.addOverlay(polyline);
                map.setViewport(pis);
            }
        }
    });


    //起点
    if(orderzhuan==null){
        initOrder();
    }
    var start = new BMap.Point(orderzhuan.startLng, orderzhuan.startLat);
    map.centerAndZoom(start,15);
    var startFlag = false;
    var startIcon = new BMap.Icon(ctx+"/assets/img/alwayspoint/zc_qd.png", new BMap.Size(21,29));
    startIcon.setImageSize(new BMap.Size(18,29));
    var startmarker = new BMap.Marker(start,{icon:startIcon});
    map.addOverlay(startmarker);
    var startopts = {
        position : start,    // 指定文本标注所在的地理位置
        offset   : new BMap.Size(-50, -45)    //设置文本偏移量
    }
    var startlabel = new BMap.Label(orderzhuan.startAddress, startopts);  // 创建文本标注对象
    startlabel.setStyle({
        color : "#5a5a5a",
        fontSize : "12px",
        height : "20px",
        lineHeight : "20px",
        fontFamily:"微软雅黑",
        border:"none",
        display:"none",
        padding:"0.2rem 0.4rem",
        borderRadius:"0.4rem"
    });
    map.addOverlay(startlabel);
    startmarker.addEventListener("click", function(){
        if(!startFlag){
            startlabel.setStyle({
                display:"block"

            });
            startFlag = true;
        }else{
            startlabel.setStyle({
                display:"none"
            });
            startFlag = false;
        }

    });

    if(orderzhuan==null){
        initOrder();
    }
    //终点
    var end = new BMap.Point(orderzhuan.endLng, orderzhuan.endLat);
    var endFlag = false;
    var endIcon = new BMap.Icon(ctx+"/assets/img/alwayspoint/zc_zd.png", new BMap.Size(21,29));
    endIcon.setImageSize(new BMap.Size(18,29));
    var endmarker = new BMap.Marker(end,{icon:endIcon});
    map.addOverlay(endmarker);
    //终点
    var endopts = {
        position : end,    // 指定文本标注所在的地理位置
        offset   : new BMap.Size(-50, -45)    //设置文本偏移量
    }
    var endlabel = new BMap.Label(orderzhuan.endAddress, endopts);  // 创建文本标注对象
    endlabel.setStyle({
        color : "#5a5a5a",
        fontSize : "12px",
        height : "20px",
        lineHeight : "20px",
        fontFamily:"微软雅黑",
        border:"none",
        display:"none",
        padding:"0.2rem 0.4rem",
        borderRadius:"0.4rem"

    });
    map.addOverlay(endlabel);
    endmarker.addEventListener("click", function(){
        if(!endFlag){
           endlabel.setStyle({
                display:"block"

            });
            endFlag = true;
        }else{
            endlabel.setStyle({
                display:"none"
            });
            endFlag = false;
        }

    });





    //车辆
    if(orderzhuan==null){
        initOrder();
    }
    var carpt = new BMap.Point(orderzhuan.startLng, orderzhuan.startLat);
    var carIcon = new BMap.Icon(ctx+"/assets/img/alwayspoint/zc_sj.png", new BMap.Size(20,37));
    carIcon.setImageSize(new BMap.Size(20,30));
    carmarker = new BMap.Marker(carpt,{icon:carIcon});
    map.addOverlay(carmarker);

    var caropts = {
        position : carpt,    // 指定文本标注所在的地理位置
        offset   : new BMap.Size(-50, -40)    //设置文本偏移量
    }
    carlabel = new BMap.Label("距离终点<span style='color:#fe7038;'>"+distance+"</span>,<span style='color:#fe7038;'>"+timeStr+"</span>", caropts);  // 创建文本标注对象
    carlabel.setStyle({
        color : "#5a5a5a",
        fontSize : "12px",
        height : "20px",
        lineHeight : "20px",
        fontFamily:"微软雅黑",
        border:"none",
        padding:"0.2rem 0.4rem",
        borderRadius:"0.4rem"
    });
    map.addOverlay(carlabel);


   window.setInterval(getPoint,1000);


    var getptFlag = true;
    function getPoint(){
        if(orderzhuan==null){
            initOrder();
        }
        if(getptFlag){
            getptFlag = false
           var getAjax =  $.get(ctx+'/passenger/api/rest/v4/point/findOneDriver',{"driverId":orderzhuan.employId},function(data){
               getptFlag = true;
                if(data.lastLatitude!=null && data.lastLongitude!=null){
                    carpt = new BMap.Point(data.lastLongitude, data.lastLatitude);
                }else{
                    carpt = new BMap.Point(data.longitude, data.latitude);
                }
                driving.search(carpt, end);
                carmarker.setPosition(carpt);
                carlabel.setContent("距离终点<span style='color:#fe7038;'>"+distance+"</span>,<span style='color:#fe7038;'>"+timeStr+"</span>");
                carlabel.setPosition(carpt);
                carmarker.setPosition(carpt);
            },"json");
            getAjax.error(function(){
                getptFlag = true;
            });

        }



    }






});